package com.mactivity.database;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.mactivity.helper.Constants;

/**
 * Contains the calls to the database functions used in registration.
 * 
 * @author Shwetha Narayan
 * 
 */
public class RegistrationFunctions {

	/**
	 * Checks the uniqueness of the email id being registered to the system.
	 * 
	 * @param emailId
	 *            Email id entered by the user.
	 * @return 0 if the email Id entered is already registered with the system,
	 *         suggesting that it cannot be used else returns 1
	 */
	public String checkEmailIdUnique(String emailId) {

		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		postParameters.add(new BasicNameValuePair("emailID", emailId));
		String response = null;

		try {
			// Query to find if the email Id is already registered.
			response = CustomHttpClient
					.executeHttpPost(
							Constants.DATABASE_URL+"Registration/reg_emailchk_script.php",
							postParameters);

		} catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,
					"Error in http connection in Registration" + e.toString());
		}

		return response;
	}

	/**
	 * Inserts the user into the system when all the checks of validity are
	 * complete.
	 * 
	 * @param emailId
	 *            Email Id entered by the user.
	 * @param name
	 *            Full name entered by the user.
	 * @param password
	 *            Password entered by the user.
	 * @return
	 */
	public void registerUser(String emailId, String name, String password) {

		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		// else insert a new record
		postParameters.add(new BasicNameValuePair("emailID", emailId));
		postParameters.add(new BasicNameValuePair("fullName", name));
		postParameters.add(new BasicNameValuePair("passWord", password));

		try {
			CustomHttpClient
					.executeHttpPost(
							Constants.DATABASE_URL+"Registration/registration_script.php",
							postParameters);

		} catch (Exception e) {
			Log.e(Constants.LOGGER_ERROR_TAG,
					"Error in http connection in Registration" + e.toString());
		}

	}
}
